﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data;
namespace Data
{
    public class TheLoaiRepository
    {        
        public static List<TheLoaiDTO> GetTheLoaiList()
        {
            NhaSachEntities context = new NhaSachEntities();
            var query = from ls in context.TheLoaiList
                        select new TheLoaiDTO { 
                            MaTheLoai=(int)ls.MaTheLoai,
                            TenTheLoai=ls.TenTheLoai
                        };
           
           
            return query.ToList();
            

        }
        public static DataTable GetTheLoaiTable()
        {
            DataTable kq = new DataTable();
            NhaSachEntities context = new NhaSachEntities();
            var query = from ls in context.TheLoaiList                        
                        select new TheLoaiDTO
                        {
                            MaTheLoai = (int)ls.MaTheLoai,
                            TenTheLoai = ls.TenTheLoai
                        };
            return ConvertTo.ListToDataTable(query);
        }
        public static TheLoaiDTO GetTheLoaiByMaTheLoai(int maTheLoai)
        {
            NhaSachEntities context = new NhaSachEntities();
            var query = (from ls in context.TheLoaiList
                        where ls.MaTheLoai == maTheLoai
                        select ls).First();
            TheLoaiDTO kq = new TheLoaiDTO
            {
                MaTheLoai = (int)query.MaTheLoai,
                TenTheLoai = query.TenTheLoai
                
            };
            return kq;
        }
        public static bool DeleteTheLoai(int maTheLoai)
        {
            NhaSachEntities context = new NhaSachEntities();
            var query = (from ls in context.TheLoaiList
                         where ls.MaTheLoai == maTheLoai
                         select ls).Single();
            return true;
            
        }
        public static bool CheckDelete(int ma)
        {
            NhaSachEntities context = new NhaSachEntities();
            var count = (from s in context.SachList
                         where s.TheLoai.MaTheLoai==ma
                         select s).Count();
            if (count!=0)
            {
                return false;
            }
            return true;
        }
        
        public static void Delele(int ma)
        {
            NhaSachEntities context = new NhaSachEntities();
            var q = (from ls in context.TheLoaiList
                     where ls.MaTheLoai == ma
                     select ls).First();
            
                context.DeleteObject(q);
                context.SaveChanges();
            
        }
        public static void Update(TheLoaiDTO TheLoai)
        {
            NhaSachEntities context = new NhaSachEntities();
            var q = (from ls in context.TheLoaiList
                     where ls.MaTheLoai == TheLoai.MaTheLoai
                     select ls).First();
            q.TenTheLoai = TheLoai.TenTheLoai;
            context.SaveChanges();
        }
        public static void Insert(TheLoaiDTO TheLoai)
        {
            NhaSachEntities context = new NhaSachEntities();
            TheLoai ls = new TheLoai()
            {
                MaTheLoai = TheLoai.MaTheLoai,
                TenTheLoai = TheLoai.TenTheLoai                
                
            };
            context.AddToTheLoaiList(ls);
            context.SaveChanges();
        }
    }
}
